package team.se.tms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import team.se.tms.annotation.Regex;
import team.se.tms.dao.Dao;
import team.se.tms.dto.Page;
import team.se.tms.dto.ParameterPage;
import team.se.tms.dto.UserDetail;
import team.se.tms.po.UserInfo;
import team.se.tms.service.UserInfoService;
import team.se.tms.util.EncryptUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息业务层，具体实现用户信息业务接口层定义的方法
 *
 * @author winkyle
 * @date 2018/11/6
 */
@Slf4j
@Service("UserInfoServiceImpl")
public class UserInfoServiceImpl implements UserInfoService{

    @Resource(name = "DaoImpl")
    private Dao dao;

    /**
     * 根据帐号获取用户信息
     *
     * @param account 帐号
     * @return 用户信息
     * @throws Exception 异常
     * @create on 2018/11/15 by winkyle
     */
    @Override
    public UserInfo getUserInfo(@Regex("^[a-zA-Z0-9]{8,16}$")String account) throws Exception {
        return dao.selectOne("UserInfoMapper.selectUserInfoByAccount",account);
    }

    /**
     * 根据用户id获取用户信息
     *
     * @param userId 用户id
     * @return 用户信息
     * @throws Exception 异常
     * @create on 2018/12/2 by qingx
     */
    @Override
    public UserInfo getUserState(String userId) throws Exception {
        UserInfo user = dao.selectOne("UserInfoMapper.selectUserStateByUserId",userId);
        return user;
    }

    /**
     * 方法描述
     *
     * @param userId 用户id
     * @param oldPassword 用户原始密码
     * @param newPassword 用户新密码
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/12/22 by johnny
     */
    @Override
    public Boolean modifyUserPassword(String userId, String oldPassword, String newPassword) throws Exception{
        //获取加密后的原始密码
        oldPassword = EncryptUtil.sha1(oldPassword,userId);
        //获取加密后的新密码
        newPassword = EncryptUtil.sha1(newPassword,userId);
        Map<String,String> map = new HashMap<>();
        map.put("userId",userId);
        map.put("oldPassword",oldPassword);
        map.put("newPassword",newPassword);
        //更新用户密码
        int result = dao.update("UserInfoMapper.updateUserPasswordByUserId",map);
        if(result>0){
            return true;
        }
        return false;
    }

    @Override
    public List<UserDetail> listUserInfos(Page page, int type, String id, String info) throws Exception {
        List<UserDetail> userDetails = new ArrayList<>();
        ParameterPage parameterPage = new ParameterPage();
        parameterPage.put("id",id);
        parameterPage.put("info",info);
        parameterPage.setPage(page);
        System.out.println("id====:"+id+"==========info"+info);
        if(type==0){
            userDetails = dao.selectList("selectStuDetailByAttr",parameterPage);
        }
        if(type==1){
            userDetails = dao.selectList("selectTeaDetailByAttr",parameterPage);
        }
        return userDetails;
    }

    @Override
    public List<UserDetail> listUserInfos(Page page) throws Exception {
        return dao.selectList("selectUserDetail",page);
    }

    @Override
    public Boolean modifyUserInfos(List<String> userIds, String roleId) throws Exception {
        /*List<UserInfo> userInfos = new ArrayList<>();
        for (int i = 0; i < userIds.size(); i++) {
            UserInfo userInfo = new UserInfo();
            userInfo.setRoleId(roleId);
            userInfo.setUserId(userIds.get(i));
        }*/
        Map map = new HashMap();
        map.put("userIds",userIds);
        map.put("roleId",roleId);
        int count = dao.update("UserInfoMapper.updateUserInfoByUserIds",map);
        if(count>0){
            return true;
        }
        return false;
    }

    /**
     * 录入用户信息
     * @param userInfo
     * @return
     * @throws Exception
     * @create on 2018/12/31 by qingx
     */
    @Override
    public boolean savaUserInfo(UserInfo userInfo) throws Exception {
//        if(multipartFile==null){
//            System.out.println("===========================文件为空");
//        }
//        UserInfo user = ModelFactory.createUserInfo(userInfo,multipartFile);
        userInfo.setUserPassword(EncryptUtil.sha1(userInfo.getUserPassword(),userInfo.getUserId()));
        userInfo.setUserHeadPath("avatar/default.jpg");
        userInfo.setUserSkin("no-skin");
        System.out.println(userInfo);
        int result = dao.insert("UserInfoMapper.insertUserInfo",userInfo);
        if (result != 0){
            return true;
        }
        return false;
    }
}
